import random
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 模拟欧洲轮盘的红黑投注，使用马丁格尔投注策略的参数设置
initial_balance = 100
target_balance = 200
initial_bet_amount = 1
win_probability = 18/37
num_simulations = 20
max_rounds = 10000

# 模拟
plt.figure(figsize=(10, 6))

for _ in range(num_simulations):
    player_balance = initial_balance
    bet_amount = initial_bet_amount
    balance_history = [player_balance]
    rounds_played = 0
    # 模拟赌局直到达到目标余额、破产或达到最大轮
    while 0 < player_balance < target_balance and rounds_played < max_rounds:

        if random.random() < win_probability:  # 玩家赢
            player_balance += bet_amount
            bet_amount = initial_bet_amount  # 获胜后重置下注金额
        else:  # 玩家输
            player_balance -= bet_amount
            bet_amount *= 2  # 输掉后加倍下注金额

        balance_history.append(player_balance)
        rounds_played += 1

        # 检查玩家是否能承担下一个赌注
        if player_balance < bet_amount:
            break

    plt.plot(balance_history)

# 绘制结果
plt.axhline(y=initial_balance, color='grey', linestyle='--', label='初始余额')
plt.axhline(y=0, color='red', linestyle='--', label='破产 (0)')
plt.axhline(y=target_balance, color='green', linestyle='--', label='目标余额 (200)')
plt.xlabel('回合')
plt.ylabel('玩家账户余额')
plt.title('赌徒破产和马格丁投注模拟: 红黑欧洲轮盘赌注')
plt.legend()
plt.show()